package com.why.controller;
import com.why.domain.Student;
import com.why.service.StudentService;
import com.why.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2024-06-14 19:33:19
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public Result addStudent(@RequestBody Student student) {
        studentService.save(student);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteStudent(@PathVariable Long id) {
        studentService.removeById(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result callStudent(@PathVariable Long id) {
        studentService.lambdaUpdate().eq(Student::getId, id).set(Student::getHasCalled, 1).update();
        return Result.success();
    }

    @GetMapping("/{groupId}")
    public Result getStudent(@PathVariable Long groupId) {
        List<Student> list = studentService.lambdaQuery().eq(Student::getGroupId, groupId).list();
        return Result.success(list);
    }

    @PostMapping("/newRound/{groupId}")
    public Result newRound(@PathVariable Long groupId) {
        studentService.lambdaUpdate().eq(Student::getGroupId, groupId).set(Student::getHasCalled, 0).update();
        return Result.success();
    }
}

